#include "optionmodel.h"

#include "collections/optioncollection.h"
#include "other/modelcommon.h"

OptionModel::OptionModel(const OptionCollection *options, QObject *parent) :
    KeyBasedListModel(parent)
{
    Q_ASSERT(options);
    _options = options;

    connect(_options, SIGNAL(elementAdded(QString)),this,
            SLOT(addKey(QString)));
    connect(_options, SIGNAL(elementUpdated(QString,QString)),
            this, SLOT(updateKey(QString,QString)));
    connect(_options, SIGNAL(elementRemoved(QString)), this,
            SLOT(removeKey(QString)));
    connect(_options, SIGNAL(cleared()), this, SLOT(clearKeys()));
}

QVariant OptionModel::getDataForKey(const QString &key, int column, int role) const {
    if(column != 0)
        return QVariant();
    switch(role) {
    case Qt::DisplayRole: {
        UnitOption option = _options->get(key);
        return option.getName();
    }
    case ModelCommon::CategoryRole: {
        UnitOption option = _options->get(key);
        return option.category;
    }
    default:
        return QVariant();
    }
}
